<?php
namespace App\Http\Controllers\Agent;
use App\Http\Controllers\AgentController;
use Illuminate\Http\Request;
use App\Models\Set;
use App\Models\UpLog;
use App\Models\Order;
use App\Models\Agent;
use App\Models\With as TWith;

class With extends AgentController{
    public function index(Request $req){
        $set = Set::getSet('base');
        $data = array('min'=>$set['minwith']??0,'rule'=>$set['withrule']??'');
        $data['ucredit'] = round(Order::where('aid',$req->agent->id)->where('status',1)->sum('reback')+Order::where('paid',$req->agent->id)->where('status',1)->sum('preback'),2);
        $data['credit'] = round(Order::where('aid',$req->agent->id)->where('status',2)->where('wid',0)->sum('reback')+Order::where('paid',$req->agent->id)->where('status',2)->where('pwid',0)->sum('preback')+UpLog::where('paid',$req->agent->id)->where('wid',0)->sum('credit'),2);
        if ($req->agent->alipayinfo) $data['info'] = json_decode($req->agent->alipayinfo,1);
        wxresp(1,$data);
    }
    
    public function save(Request $req){
        if ($req->post('op') == 'info') {
            Agent::where('id',$req->agent->id)->update(array('alipayinfo'=>json_encode(array('name'=>$req->post('name'),'account'=>$req->post('account')))));
            wxresp(1);
        }
        $credit = 0;
        $ids = [[],[],[]];
        $list = Order::where('aid',$req->agent->id)->where('status',2)->where('wid',0)->select('reback','id')->get();
        foreach ($list as $v) {
            $credit += $v->reback;
            $ids[0][] = $v->id;
        }
        $plist = Order::where('paid',$req->agent->id)->where('status',2)->where('pwid',0)->select('preback','id')->get();
        foreach ($plist as $v) {
            $credit += $v->preback;
            $ids[1][] = $v->id;
        }
        $ulist = UpLog::where('paid',$req->agent->id)->where('wid',0)->select('credit','id')->get();
        foreach ($ulist as $v) {
            $credit += $v->credit;
            $ids[2][] = $v->id;
        }
        if ($credit <= 0) wxresp(0,'没有可提现金额');
        $set = Set::getSet('base');
        if ($set['minwith'] > 0 && $credit < $set['minwith']) wxresp(0,'最低提现金额：'.$set['minwith']);
        $wid = TWith::insertGetId(array('aid'=>$req->agent->id,'credit'=>$credit,'createtime'=>time()));
        if ($ids[0]) Order::whereIn('id',$ids[0])->update(array('wid'=>$wid));
        if ($ids[1]) Order::whereIn('id',$ids[1])->update(array('pwid'=>$wid));
        if ($ids[2]) UpLog::whereIn('id',$ids[2])->update(array('wid'=>$wid));
        wxresp(1);
    }
    
    public function log(Request $req){
        $page = $req->post('page',1);
        $size = 15;
        $list = TWith::where('aid',$req->agent->id)->orderByDesc('id')->offset(($page-1)*$size)->limit($size)->get();
        $datas = [];
        foreach ($list as &$v) {
            $label = '待审核';
            if ($v->status == 1) $label = '已发放';
            elseif ($v->status == -1) $label = '已拒绝：'.$v->reason;
            $datas[] = array('createtime'=>date('Y-m-d H:i',$v->createtime),'price'=>$v->credit,'label'=>$label);
        }
        wxresp(1,['list'=>$datas]);
    }
}